package com.briup.impl.util;

import com.briup.inter.util.Backup;

import java.io.*;

public class BackupImpl implements Backup {
    @Override
    public Object load(String filePath, boolean del) throws Exception {
        Object o = null;
        File file = new File(filePath);
        if(!file.exists()){
            return null;
        }
        try (FileInputStream fis = new FileInputStream(filePath);
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            o = ois.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }
        // del 是true 加载删除文件
        if (del){
            file.delete();
        }
        return o;
    }

    @Override
    public void store(String filePath, Object obj, boolean append) throws Exception {
        // 本地路径
        try (FileOutputStream fos = new FileOutputStream(filePath, append);
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(obj);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
